<?php
/***
 * 描述：商品推广
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: SpreadManageAction.class.php 77 2013-12-01 15:01:45Z whtaok@gmail.com $
 */
class SpreadManageAction extends AdminAction {
	private $savePath = '';
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->savePath = C('REALPATH_SHOP').'/'.$this->curr_shop_id.'/';
	}
	
	/**
	 * 显示推广管理页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$list = array();	
		//1.1.显示热门商品
		$type = 'hot';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "热销商品";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));
		
		//1.2.显示推荐商品
		$type = 'rmd';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "商家促销";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));
		
		//1.3.显示新商品
		$type = 'new';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "新品上架";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));

		//1.3.显示新商品
		$type = 'frn';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "进口商品";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));		
		
		//3.显示推荐的食品粮油商品-A1
		$type = 'A1';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页食品粮油";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));	

		//4.显示推荐的酒水饮料商品
		$type = 'B1';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页酒水饮料";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));	

		//5.显示推荐的日用百货商品
		$type = 'C1';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页日用百货";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));

		//6.显示推荐的套餐盒饭商品
		$type = 'D1';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页套餐盒饭";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));
		
		//7.显示推荐的干果-A1
		$type = 'A107';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页干果干货";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));
		
		//8.显示推荐的进口食品-A203
		$type = 'A203';
		$cacheData = F_CACHE($this->savePath.$type);
		$list[$type]['name'] = "首页进口食品";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));
		
		
		//3.显示首页团购商品
		$type = 'tg';
		$cacheData = F_CACHE(C('REALPATH_SHOP').'/'.$type);
		$list[$type]['name'] = "首页团购";
		if(is_array($cacheData)){
			$list[$type]['num'] = count($cacheData);
		}else{
			$list[$type]['num'] = 0;
		}
		$list[$type]['act_detail'] = U('showDetailCache',array('type'=>$type));
		$list[$type]['act_add'] = U('doAddCache',array('type'=>$type));
		$list[$type]['act_del'] = U('doDelCache',array('type'=>$type));		
		//4.显示首页广告信息
		
		$this->assign('list',$list);
		$this->display('index');
	}
	
	/**
	 * 删除缓存热门/推荐商品/团购商品列表页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showDetailCacheAct($type){
		switch($type){
			case 'tg' :
				$cacheData = F_CACHE(C('REALPATH_SHOP').'/'.$type);
				break;
			default:
				$cacheData = F_CACHE($this->savePath.$type);
				$type = 'hot';
		}		
		$this->assign('list',$cacheData);
		exit($this->fetch('list_'.$type));
		
	}	
	
	/**
	 * 生成缓存热门/推荐商品列表页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */	
	public function doAddCacheAct(){
		$type = I('type');
		switch($type){
			case 'tg' :
				$savePath = C('REALPATH_SHOP').'/'.$type;
				$typeName = "首页团购";
				$viewMod = new TgProdLibModel();
				list($list,$page)=$viewMod->findHotRmdListPage(1,0);
				break;
			case 'hot':
				$savePath = $this->savePath.$type;
				$typeName = "热门商品";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot');
				break;
			case 'rmd':
				$savePath = $this->savePath.$type;
				$typeName = "推荐商品";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_recommend');
				break;
			case 'new':
				$savePath = $this->savePath.$type;
				$typeName = "新品上架";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_new');
				break;
			case 'frn':
				$savePath = $this->savePath.$type;
				$typeName = "进口商品";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_foreign');
				break;
			case 'A1':
				$savePath = $this->savePath.$type;
				$typeName = "首页热门食品粮油";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','A1');
				break;
			case 'B1':
				$savePath = $this->savePath.$type;
				$typeName = "首页热酒水饮料";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','B1');
				break;
			case 'C1':
				$savePath = $this->savePath.$type;
				$typeName = "首页热门日用百货";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','C1');
				break;
			case 'D1':
				$savePath = $this->savePath.$type;
				$typeName = "首页热门套餐盒饭";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','D1');
				break;
			case 'A107':
				$savePath = $this->savePath.$type;
				$typeName = "首页热门干果干货";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','A107');
				break;	
			case 'A203':
				$savePath = $this->savePath.$type;
				$typeName = "首页热门进口食品";
				$viewMod = new ProductPriceViewModel();
				list($list,$page)=$viewMod->findActiveStatusListPage($this->curr_shop_id,'is_hot','A203');
				break;
			default:
				$this->error('缓存数据生成失败');
		}
		if(isset($list) && count($list)>0){
			$cnt = count($list)>=20?20:count($list);
			$cacheData = array();
			for ($i=0 ; $i<$cnt ; $i++){
				$data = $list[$i];
				if($type == 'tg'){
					$cacheData[$data['id']] = array(
							'prod_id' => $data['id'],
							'name' => $data['name'],
							'spec' => m_prod_spec( $data['spec'], $data['spec_qty'], $data['spec_unit']),
							'price_real' => $data['price_real'],
							'price_sale' => $data['price_sale'],
							'is_hot' => $data['is_hot'],
							'is_recommend' => $data['is_recommend'],
					);
				}else{
					$cacheData[$data['prod_id']] = array(
							'prod_id' => $data['prod_id'],
							'name' => $data['name'],
							'spec' => m_prod_spec( $data['spec'], $data['spec_qty'], $data['spec_unit']),
							'price_real' => $data['price_real'],
							'price_sale' => $data['price_sale'],
							'is_hot' => $data['is_hot'],
							'is_recommend' => $data['is_recommend'],
							'is_new' => $data['is_new'],
							'is_foreign' => $data['is_foreign'],
					);
				}

			}
			F_CACHE($savePath,$cacheData);
			unset($list);
			unset($page);
			$this->success($typeName.'缓存数据生成成功');
		}else{
			$this->error($typeName.'缓存数据生成发生错误');
		}
	}
	
	/**
	 * 删除缓商品列表页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelCacheAct(){
		$type = I('type');
		switch($type){
			case 'tg' :
				F_CACHE(C('REALPATH_SHOP').'/'.$type,null);
				$this->success('首页团购商品数据已从缓存中删除');
				break;
			default:
				F_CACHE($this->savePath.$type,null);
				$this->success('商品数据已从缓存中删除');
		}
	}

}

?>